// miniprogram/pages/index/company/start_speech/start_speech.js
const db=wx.cloud.database()
const _=db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company:{},
    btn: true
  },
  check: function(event){
    const that = this;
    let tem="";
    wx.requestSubscribeMessage({
      tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
      success: res=>{
        tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
        if(tem=="accept"){
            wx.showToast({
            title: '已开启',
          })
          that.startSpeedch(event);
        }else{
          wx.showToast({
            title: '已拒绝',
          })
        }
      },
      fail: res=>{
          wx.showToast({
            title: '已拒绝',
          })
      }
    })
  },
  startSpeedch: function(event){
    this.setData({btn: !this.data.btn})
    const company_obj=this.data.company.company_obj;

   const speech_name=event.detail.value.speech_name;
   const speech_date=event.detail.value.speech_date;
   const speech_size=event.detail.value.speech_size;
   const speech_devices=event.detail.value.speech_devices;
   const speech_main=event.detail.value.speech_main;
   const speech_job=event.detail.value.speech_job;
   
   const openid=company_obj.openid;
   const cpny_name=company_obj.cpny_name;
   const cpny_location=company_obj.cpny_location;
   const cpny_man=company_obj.cpny_man;
   const cpny_phone=company_obj.cpny_phone;
   const cpny_mail=company_obj.cpny_mail;
   const cpny_profile=company_obj.cpny_profile;
   const cpny_type=company_obj.cpny_type;
   const images=company_obj.images;
    
   const today = new Date();
   const year = today.getFullYear();
   const mouth = today.getMonth() + 1;
   const day = today.getDate();
   let mouth_str='';
   let day_str='';
   if(mouth<10){
    mouth_str="0"+mouth;
   }else{ 
     mouth_str=mouth;
   }
   if(day<10){
     day_str="0"+day;
    }else{ 
      day_str=day;
    }
    const date_str=year+"-"+mouth_str+"-"+day_str;

    const speech_obj={
    speech_name: speech_name,
    speech_date:speech_date,
    speech_size:speech_size,
    speech_devices:speech_devices,
    speech_main:speech_main,
    speech_job:speech_job,
   
    openid:openid,
    cpny_name:cpny_name,
    cpny_location:cpny_location,
    cpny_man:cpny_man,
    cpny_phone:cpny_phone,
   cpny_mail:cpny_mail,
    cpny_profile:cpny_profile,
    cpny_type:cpny_type,
    images:images,

    speech_type: -1,
    speech_note: '',
    speech_start_date: date_str

    
    }
    db.collection("speech").add({
      data: {
       
        speech_obj: speech_obj
      }
     
    }).then(res=>{
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: 'oAuf35SpOxe24BaPIPUZ4_2wDRXU',
          title: "宣讲会",
          content: cpny_name,
          note:  speech_name
        }
      })
     getApp().tos("发布成功")
      setTimeout(function () {
         wx.navigateBack()
       }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      company: JSON.parse(decodeURIComponent(options.obj))
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({btn: !this.data.btn})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})